function ltrim(str) {
    for (var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
    return str.substring(k, str.length);
}

function rtrim(str) {
    for (var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--);
    return str.substring(0,j+1);
}

function trim(str) {
    return ltrim(rtrim(str));
}

function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
} 

/* 
 * valor en options?
 * options es el arreglo de opciones del control select (para telefonos y correos)
 */
function inArray(valor, options){
    if (!(valor == null || valor == "")){
        for (i = 0; i < options.length; i++){
            if (valor == options[i].value)
                return true;
        }
    }
    return false;
}

//Devuelve la url base del sitio
function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function validarFormularioBecarios(form){
    if(form.carnet.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.nombres.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message2" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.apellido.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message3" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.sexo.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message4" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.estCivil.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message5" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.fechaNac.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message6" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.lugarNac.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message7" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.dirActual.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message8" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.dirOrigen.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message9" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.mcipioOrigen.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message10" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.deptoOrigen.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message11" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.fondo.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message12" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.phones.options.length == 0){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message13" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.codUniversidad.value.toString().trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message14" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.codCarrera.value.toString().trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message15" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.cum.value < 0 || form.cum.value > 10){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message16" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.fechaIniCarrera.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message17" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.estado.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message18" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.cma.value < 0){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message19" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.instEduMedia.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message20" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    if(form.dirIem.value.trim() == ""){
        $( "#dialog:ui-dialog" ).dialog( "destroy" );
        $( "#dialog-message21" ).dialog({minHeight: 70, height: 180, modal: true, buttons: {Ok: function() {$( this ).dialog( "close" );}}});
        return false;
    }
    return true;
}

